package com.gdpu.controller;

import com.gdpu.api.CommonPage;
import com.gdpu.api.CommonResult;
import com.gdpu.dao.UmsAddressDao;
import com.gdpu.mapper.UmsAddressMapper;
import com.gdpu.model.UmsAddress;
import com.gdpu.service.UmsAddressService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:weiming
 * @Date:2021/6/7
 */
@Api(tags = "用户地址模块")
@RestController
@RequestMapping("/address")
public class UmsAddressController {

    @Autowired
    private UmsAddressService umsAddressService;

    @Autowired
    private UmsAddressDao umsAddressDao;


    @ApiOperation("获取地址列表")
    @GetMapping("/")
    public CommonResult get(@RequestParam(name ="pageNum" ,defaultValue = "0") Integer pageNum,
                            @RequestParam(name ="pageSize" ,defaultValue = "10") Integer pageSize){
        CommonPage commonPage = umsAddressService.getList(pageNum, pageSize);
        if (commonPage!=null){
            return CommonResult.success(commonPage);
        }
        return  CommonResult.failed();
    }

    @ApiOperation("获取地址详情")
    @GetMapping("/{id}")
    public CommonResult get(@PathVariable("id")Integer id){
        UmsAddress umsAddress = umsAddressService.get(id);
        if (umsAddress!=null){
            return CommonResult.success(umsAddress);
        }
        return  CommonResult.failed();
    }

    @ApiOperation("新增地址")
    @PostMapping("/")
    public CommonResult add(@RequestBody UmsAddress umsAddress){
        int count = umsAddressService.add(umsAddress);
        if (count > 0){
            return CommonResult.success("新增地址成功");
        }
        return CommonResult.failed();
    }
    @ApiOperation("更新地址")
    @PutMapping("/")
    public CommonResult update(@RequestBody UmsAddress umsAddress){
        Integer count = umsAddressService.update(umsAddress);
        if (count > 0){
            return CommonResult.success("更新地址成功");
        }
        return CommonResult.failed();
    }

    @ApiOperation("设置为默认地址")
    @PutMapping ("/default")
    public CommonResult setDefaultAddress(){
        // 设置为默认地址
        int count = umsAddressService.setDefaultAddress();
        if (count > 0){
          return   CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PutMapping ("/test")
    public CommonResult  test(){
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);

        Page page = PageHelper.startPage(0,2);
        Map<String, Object> map = new HashMap<>(1);
        map.put("user_id", 1);
        List<UmsAddress> umsAddresses = umsAddressDao.selectByMap(map);
        for (UmsAddress umsAddress : umsAddresses) {
            System.out.println(umsAddress);
        }
        CommonPage<UmsAddress> commonPage = CommonPage.restPage(umsAddresses, page);
        return CommonResult.success(commonPage);
    }

}
